require "Net/send_Msg"
require "Net/TrialHandel"
require 'View/UIBase/UIBase'

UIChaosTestRank = UIBase.New(PanelNames.UIChaosTestRank);
local rankCount = 5;
local change_type = nil
function UIChaosTestRank.Init(func, _change_type)
    change_type = _change_type or 1
    UIBase.Init(UIChaosTestRank, func);
end

--定义控件
function UIChaosTestRank:OnCreate()
    self.tops = {}
    for i = 1, 1 do
        local item = self:GetChild("Top" .. i);
        table.insert(self.tops, item);
    end
    self.listInfo = {};
    self.myInfo = {};
    self.myRankCom = self:GetChild("MyRank");
    self.rankList = self:GetChild("RankList");
    self.backBtn = self:GetChild("BackButton");
    self.rankTypeController = self:GetController("c1");
    self:addEvent();
    if change_type == 1 then
        self:RegisterEvent(Config.EventType.Trials_Chaos_Friend_Rank, self.changePage);
        self:RegisterEvent(Config.EventType.FreashRoleInfoOther, self.GotOtherShowInfo);
    else
        -- todo
    end
    --self.CloseFom();
end

function UIChaosTestRank:OnEnable()
    if change_type == 1 then
        TrialHandel.SendTrialsWorldRank()
        TrialHandel.SendTrialsFriendRank()
    else
        -- todo
    end
end

function UIChaosTestRank:OnDisable()

end

function UIChaosTestRank:OnDestroy()
    if change_type == 1 then
        self:UnRegisterEvent(Config.EventType.Trials_Chaos_Work_Rank, self.setInfo, self);
        self:UnRegisterEvent(Config.EventType.Trials_Chaos_Friend_Rank, self.changePage, self);
        self:UnRegisterEvent(Config.EventType.FreashRoleInfoOther, self.GotOtherShowInfo);
    else
        -- todo
    end

    self.rankTypeController.onChanged:Clear();

end

function UIChaosTestRank:addEvent()
    self:RegisterOnClick(self.backBtn, self.onCloseClick)
    self.rankTypeController.onChanged:Add(self.changePage);
end

function UIChaosTestRank:changePage()
    local self = UIChaosTestRank;
    local rankInfo = nil;

    if change_type == 1 then  
        self.myInfo = logicMgr.TrialsManager.GetMyRankInfo();
        if self.rankTypeController.selectedIndex == 0 then
            rankInfo = logicMgr.TrialsManager.GetWorkRank();
            if rankInfo then
                self.listInfo = rankInfo;
                self:setInfo();
            else
                TrialHandel.SendTrialsWorldRank();
            end
            --世界排行
        else
            rankInfo = logicMgr.TrialsManager.GetFriendRank();
            --好友排行
            if rankInfo then
                self.listInfo = rankInfo;
                self:setInfo();
            else
                TrialHandel.SendTrialsFriendRank();
            end
        end 
        self.rankList:ScrollToView(0)
    else
        -- todo
    end 
end

function UIChaosTestRank:removeEvent()
    ----bt_SetBtn.onClick:Clear();
    --self.backBtn.onClick:Clear();
    --self.bg.onClick:Clear();
    --self.lastBtn.onClick:Clear();
    --self.nextBtn.onClick:Clear();
    --self.titleDecBackBtn.onClick:Clear();
end

function UIChaosTestRank:onCloseClick()
    closeUI(PanelNames.UIChaosTestRank);
end

function UIChaosTestRank:setInfo()
    --初始化玩家个人信息
    local myRank = self.myRankCom:GetChild("MyRank");
    local myName = self.myRankCom:GetChild("PlayerName");
    local myDyDamage = self.myRankCom:GetChild("PlayerDmg");
    local muRankIcon = self.myRankCom:GetChild("MyRankIcon");
    local myFrame = self.myRankCom:GetChild("Head"):GetChild("frame");
    local myLevel = self.myRankCom:GetChild("Level");
    local recordBtn = self.myRankCom:GetChild("Record");
    local rankCtrl = self.myRankCom:GetController("ranking");
    local Rank = self.myRankCom:GetChild("Rank");
    if rankCount > 3 then
        rankCtrl.selectedIndex = 3;
    else
        rankCtrl.selectedIndex = rankCount - 1;
    end
    local recodeData = logicMgr.TrialsManager.GetTrialsRecordInfo();
    recordBtn.onClick:Clear();
    recordBtn.onClick:Add(function()
        if recodeData.records ~= nil and recodeData.records[1] then
            showUI(PanelNames.UILevelRecode, EnumConst.FightTypeConst.TRIAL, recodeData.records[1])
        else
            UISysTips.AddMsg(getLanguage("ChaoTips2"));
        end
    end)
    local headIcon = self.myRankCom:GetChild("Head"):GetChild("head"):GetChild("HeadIcon");
    headIcon.url = logicMgr.SkinManager.GetHeroIconBySkinId(G.serverData["roleInfo"].show.pic)
    if self.rankTypeController.selectedIndex == 0 then
        if self.myInfo.workRank > 0 then
            if self.myInfo.workRank < 4 then
                muRankIcon.visible = true;
                rankCtrl.selectedIndex = self.myInfo.workRank - 1;
                Rank.text = "";
            else
                muRankIcon.visible = false;
                Rank.text = self.myInfo.workRank;
            end
        else
            muRankIcon.visible = false;
            Rank.text = getLanguage("NotInRank");
        end
    else
        if self.myInfo.friendRank > 0 then
            if self.myInfo.friendRank < 4 then
                muRankIcon.visible = true;
                rankCtrl.selectedIndex = self.myInfo.friendRank - 1;
                Rank.text = "";
            else
                muRankIcon.visible = false;
                Rank.text = self.myInfo.friendRank;

            end

        else
            muRankIcon.visible = false;
            Rank.text = getLanguage("NotInRank");
        end
    end
    myName.text = G.serverData["roleInfo"].show.name;
    myDyDamage.text =getNumString(self.myInfo.damage);
    myLevel.text = self.myInfo.level;
    myFrame.url = "ui://ICONAtlas/" .. G.serverData["roleInfo"].show.picFrame;
    rankCount = 5;
    local rankNum = #self.listInfo - #self.tops;
    local topNum = 0;
    if rankNum > 0 then
        topNum = 5
    else
        topNum = #self.listInfo;
    end
    for i = 1, #self.tops do
        local data = self.listInfo[i];
        local item = self.tops[i];
        local controller = item:GetController("Player");
        if topNum >= i then
            controller.selectedIndex = 0;
            local Name = item:GetChild("PlayerName");
            local playerLevel = item:GetChild("PlayerLayers");
            local DyDamage = item:GetChild("PlayerDmg");
            local recordBtn = item:GetChild("Record");
            recordBtn.onClick:Clear();
            recordBtn.onClick:Add(function()
                if data.record then
                    showUI(PanelNames.UILevelRecode, EnumConst.FightTypeConst.TRIAL, data.record)
                else
                    UISysTips.AddMsg(getLanguage("ChaoError1"));
                end
            end)

            local cIndex=0
            if(i==1)then
                cIndex=1
            end
            item:GetChild("RankHead"):GetController("first").selectedIndex=cIndex

            local headIcon = item:GetChild("RankHead"):GetChild("head"):GetChild("HeadIcon");
            local frameIcon = item:GetChild("RankHead"):GetChild("frame");
            headIcon.url = logicMgr.SkinManager.GetHeroIconBySkinId(data.playerShowInfo.pic)
            frameIcon.url = "ui://ICONAtlas/" .. data.playerShowInfo.picFrame;
            item:GetChild("RankHead").onClick:Clear();
            item:GetChild("RankHead").onClick:Add(function()
                if tostring(data.playerShowInfo.playerId) == HeroManager.GetRoleId() then
                    return ;
                end
                roleInfoHandler.sendreqOtherRoleInfo(data.playerShowInfo.playerId);
            end)
            --local Level = item:GetChild("RankHead"):GetChild("level");

            playerLevel.text = data.level
            Name.text = data.playerShowInfo.name;
            DyDamage.text =getNumString(data.damage);
            --Level.text = data.level;
        else
            controller.selectedIndex = 1;
            local headIcon = item:GetChild("RankHead"):GetChild("head"):GetChild("HeadIcon");
            headIcon.url = nil;
        end

    end
    if rankNum > 0 then
        rankCount = #self.tops + 1;
        if rankNum < 10 -#self.tops then
            rankNum = rankNum + (10- #self.tops - rankNum);
        end
        self.rankList.itemRenderer = self.rendererRankItem;
        self.rankList.numItems = Config.Const_Rank_Max_Num - 1--rankNum
        HelperFunc.ListTranstionPlay(self.rankList)
    else
        self.rankList.itemRenderer = self.rendererRankItemNone;
        self.rankList.numItems = Config.Const_Rank_Max_Num - 1--10 -#self.tops;
        HelperFunc.ListTranstionPlay(self.rankList)
    end

end

function UIChaosTestRank:GotOtherShowInfo(info)
    local cur_change_type = change_type or 1
    showUI(PanelNames.UIPlayerShow, info,nil,false,function ()
        UIBottom.enterAdventure()
        showUI(PanelNames.UIChaosTest, cur_change_type)
        showUI(PanelNames.UIChaosTestRank, cur_change_type)
    end)
end

function UIChaosTestRank:rendererRankItemNone(obj)
    obj:GetController("empty").selectedIndex = 1;
end

function UIChaosTestRank:rendererRankItem(obj)
    local self = UIChaosTestRank;

    local data = self.listInfo[rankCount];
    if data == nil then
        obj:GetController("empty").selectedIndex = 1;
        return ;
    else
        obj:GetController("empty").selectedIndex = 0;
    end
    local item = obj;

    local Rank = item:GetChild("Rank");
    local Name = item:GetChild("PlayerName");
    local DyDamage = item:GetChild("PlayerDmg");
    local Level = item:GetChild("Level");
    local headIcon = item:GetChild("Head"):GetChild("head"):GetChild("HeadIcon");
    local headBtn = item:GetChild("Head")
    local recordBtn = item:GetChild("Record");
    local frame = item:GetChild("Head"):GetChild("frame")
    local rankCtrl = item:GetController("ranking");
    if rankCount > 3 then
        rankCtrl.selectedIndex = 3;
    else
        rankCtrl.selectedIndex = rankCount - 1;
    end
    recordBtn.onClick:Clear();
    recordBtn.onClick:Add(function()
        if data.record then
            showUI(PanelNames.UILevelRecode, EnumConst.FightTypeConst.TRIAL, data.record)
        else
            UISysTips.AddMsg(getLanguage("ChaoError1"));
        end
    end)
    headBtn.onClick:Clear();
    headBtn.onClick:Add(function()
        if tostring(data.playerShowInfo.playerId) ~= logicMgr.HeroManager.GetRoleId() then
            roleInfoHandler.sendreqOtherRoleInfo(data.playerShowInfo.playerId);
        end
    end)
    headIcon.url = logicMgr.SkinManager.GetHeroIconBySkinId(data.playerShowInfo.pic)
    Rank.text = rankCount;
    Name.text = data.playerShowInfo.name;
    DyDamage.text =getNumString(data.damage) ;
    Level.text = data.level;

    frame.url = "ui://ICONAtlas/" .. data.playerShowInfo.picFrame;
    rankCount = rankCount + 1;
end

return UIChaosTestRank;